package com.hns.can.master.web;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.common.domain.GridPage;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.BaseCarInfoEntity;
import com.hns.can.master.service.CarDrvRelService;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.PubUtil;

/**
 * 驾驶员车辆分配的控制类
 * @author:lan
 * @email:763836737@qq.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/carDrvRel")
public class CarDrvRelController extends BaseController{
    
	/**
	 * 车辆司机关联service
	 */
    @Autowired
    private CarDrvRelService carDrvRelService; // 注入carDrvRelService
    
    /**
	 * 导出文件表头
	 */
    private final String[] tableHeaderNames={"车牌号", "设备ID", "驾驶员姓名", "员工工号", "考勤卡卡号", "性别", "联系方式"};
    /**
	 * 导出文件字段名
	 */
    private final String[] tableHeaderFields={"licPltNo","eqmtCd","drvName","workEmpeEmpno","timecardCardno","gender","ctcWay"};
    
    /**
     * 根据选中的汽车，查看其绑定驾驶员情况，返回grid对象。
     * @param carIds
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/searchBoundDrvPage")
    @ResponseBody
    public Object searchBoundDrvPage(@RequestParam(value = "carIds",required = false) List<String> carIds) throws Exception {
        if(PubUtil.isEmpty(carIds)){
            throw new BusinessException(MsgType.WARN.getFlag(), "请选择车辆");
        }else{
            Pagination page = carDrvRelService.searchBoundDrvPage(carIds);
            GridPage grid = new GridPage(page);
            return this.getReturnResult(grid);
        }
    }
    
    /**
     * 取得一个机构下，启用状态，未被分配的驾驶员，返回grid对象。
     * @param organId
     * @param qdrvName
     * @param qworkEmpeEmpno
     * @param qtimecardCardno
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getUnBoundDrvPage")
    @ResponseBody
    public Object getUnBoundDrvPage(
        @RequestParam(value="organId",required=false) String organId, 
        @RequestParam(value="qdrvName",required=false) String qdrvName, 
        @RequestParam(value="qworkEmpeEmpno",required=false) String qworkEmpeEmpno, 
        @RequestParam(value="qtimecardCardno",required=false) String qtimecardCardno) throws Exception {
        if(PubUtil.isEmpty(organId)){
            throw new BusinessException(MsgType.WARN.getFlag(), "请选择机构");
        }else{
            Pagination page = carDrvRelService.getUnBoundDrvPage(organId, this.getCurrentUser().getUser(), qdrvName, qworkEmpeEmpno, qtimecardCardno);
            GridPage grid = new GridPage(page);
            return this.getReturnResult(grid);
        }
    }

    /**
     * 将驾驶员绑定到车辆上
     * @param carId
     * @param drvIds
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/bindDrvsToCar")
    @ResponseBody
    public Object bindDrvsToCar(@RequestParam("carId") String carId, @RequestParam("drvIds") String[] drvIds) throws Exception {
        if(PubUtil.isNotEmpty(carId) && PubUtil.isNotEmpty(drvIds)){
            carDrvRelService.bindDrvsToCar(carId, drvIds, this.getCurrentUser().getUser());
        }
        return this.getReturnMsg("保存成功");
    }
    
    /**
     * 解除驾驶员和车辆的绑定关系
     * @param relId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/unBindDrv")
    @ResponseBody
    public Object unBindDrv(@RequestParam("relId") String relId) throws Exception {
        carDrvRelService.unBindDrv(relId);
        return this.getReturnMsg("删除成功");
    }
    
    /**
     * 通过车辆id得到车辆信息
     * @param carId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getCarInfoByCarId")
    @ResponseBody
    public Object getCarInfoByCarId(@RequestParam("carId") String carId) throws Exception {
        BaseCarInfoEntity entity = carDrvRelService.getCarInfoByCarId(carId);
        return this.getReturnResult(entity);
    }
    
    /**
     * 导出excel文件
     * @param carIds
     * @param response
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportCarDrvRelExcel")
    @ResponseBody
    public void exportCarDrvRelExcel(
        @RequestParam(value = "carIds",required = false) List<String> carIds,HttpServletResponse response) throws Exception {
        if(PubUtil.isNotEmpty(carIds)){
            List<BaseEntity> list = carDrvRelService.getBoundDrvList(carIds);
            ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "excel", "carDrvRel", "车辆司机关联");
        }else{
            throw new BusinessException(MsgType.WARN.getFlag(), "至少选择一辆车！");
        }
    }
    
    /**
     * 导出pdf文件
     * @param carIds
     * @param response
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportCarDrvRelPdf")
    @ResponseBody
    public void exportCarDrvRelPdf(
        @RequestParam(value = "carIds",required = false) List<String> carIds,HttpServletResponse response) throws Exception {
        if(PubUtil.isNotEmpty(carIds)){
            List<BaseEntity> list = carDrvRelService.getBoundDrvList(carIds);
            ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "pdf", "carDrvRel", "车辆司机关联");
        }else{
            throw new BusinessException(MsgType.WARN.getFlag(), "至少选择一辆车！");
        }
    }
    
}
